﻿using System.Text.RegularExpressions;

namespace com.uplus.common
{
    /// <summary>
    /// 提供有效性验证服务
    /// @bayeyang, 2014-06-10
    /// </summary>
    public class Check
    {
        static readonly string CHINAMOBILE = @"(134\d{8}|135\d{8}|136\d{8}|137\d{8}|138\d{8}|139\d{8}|147\d{8}|150\d{8}|151\d{8}|152\d{8}|157\d{8}|158\d{8}|159\d{8}|182\d{8}|183\d{8}|184\d{8}|187\d{8}|188\d{8})";
        static readonly string CHINAUNICOM = @"(130\d{8}|131\d{8}|132\d{8}|145\d{8}|155\d{8}|156\d{8}|185\d{8}|186\d{8})";
        static readonly string CHINATELECOM = @"(133\d{8}|153\d{8}|180\d{8}|181\d{8}|189\d{8})";
        /// <summary>
        /// 根据运营商类型验证用户输入的号码是否属于该类型的运营商
        /// </summary>
        /// <param name="match">电话号码</param>
        /// <param name="phoneNum">电话号码</param>
        /// <param name="type">运营商类型：1-中国移动，2-中国联通，</param>
        /// <returns>是否属于该类型的运营商</returns>
        public static bool CheckPhone(string phoneNum, TelecomOperator type)
        {
            Regex re = null;
            switch (type)
            {
                case TelecomOperator.ChinaMobile:
                    re = new Regex(CHINAMOBILE);
                    break;
                case TelecomOperator.ChinaUnicom:
                    re = new Regex(CHINAUNICOM);
                    break;
                case TelecomOperator.ChinaTelecom:
                    re = new Regex(CHINATELECOM);
                    break;
                case TelecomOperator.All:
                    string temp = string.Format("{0}|{1}|{2}",
                        CHINAMOBILE.Substring(0, CHINAMOBILE.Length - 1),
                        CHINAUNICOM.Substring(1, CHINAUNICOM.Length - 2),
                        CHINATELECOM.Substring(1, CHINATELECOM.Length - 1));
                    re = new Regex(temp);
                    break;
                default:
                    break;
            }
            if (re != null)
            {
                return re.IsMatch(phoneNum);
            }
            else
                return false;
        }
        /// <summary>
        /// 电信运营商
        /// </summary>
        public enum TelecomOperator
        {
            /// <summary>
            /// 中国移动
            /// </summary>
            ChinaMobile,
            /// <summary>
            /// 中国联通
            /// </summary>
            ChinaUnicom,
            /// <summary>
            /// 中国电信
            /// </summary>
            ChinaTelecom,
            /// <summary>
            /// 所有运营商
            /// </summary>
            All
        }
    }
}
